<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;
use Illuminate\Database\Eloquent\SoftDeletes;

class Comment extends Model
{
    use SoftDeletes;
    protected $fillable = ['content', 'article_id', 'member_id'];
    protected $dateFormat = 'U';

    //关联文章模型
    public function articles()
    {
        return $this->belongsTo('App\\Models\\Article', 'article_id', 'id');
    }

    //关联会员模型
    public function members()
    {
        return $this->belongsTo('App\\Models\\Member', 'member_id', 'id');
    }

    //评论
    public function add($data)
    {
        $rule = [
            'content' => 'required'
        ];
        $msg = [
            'content.required' => '评论内容不能为空'
        ];
        $validator = Validator::make($data, $rule, $msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $result = $this->create($data);
        if ($result) {
            return 1;
        }else {
            return '评论失败';
        }
    }
}
